<?php


/**
 * 接口使用输出函数
 */
if (!function_exists('mdsResponse')) {

    function mdsResponse($code,$msg = "",$result = '',$expand_data = [],$type = 1){

        $response = [
            'code' => $code,
            'msg' => $msg
        ];

        if($result != ''){

            //矫正laravel对时间的处理，找到地方以后这个地方可以撤销
            $result = json_decode(json_encode($result),true);
            //最后对$data进行处理
            foreach($result as $k=>$v){
                if(isset($v['created_at'])){
                    $result[$k]['created_at'] = date("Y-m-d H:i:s",strtotime($v['created_at']));
                }
                if(isset($v['updated_at'])){
                    $result[$k]['updated_at'] = date("Y-m-d H:i:s",strtotime($v['updated_at']));
                }
            }

            $response['data'] = $result;
        }

        if(!empty($expand_data)){
            foreach($expand_data as $k=>$v){
                $response[$k] = $v;
            }
        }

        $response_data = json_encode($response,JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);

        if($type){
            exit($response_data);
        }else{
            echo $response_data;
        }

    }

}

if (!function_exists('mds_if_response')) {

    function mds_if_response($re,$success = "成功",$fail = "失败"){

        if(gettype($re) == 'array'){
            $isRe = $re['code']??200;   //默认200
        }else{
            if($re){
                $isRe = 200;
            }else{
                $isRe = 400;
            }
            $re = ["re"=>$re];
        }
        if($isRe == 200){
            mdsResponse(200,$success,$re);
        }else{
            mdsResponse(400,$fail,$re);
        }

    }

}


if (!function_exists('uuid')) {
    /**
     * 获取全球唯一标识
     * @return string
     */
    function uuid()
    {
        return sprintf(
            '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
            mt_rand(0, 0xffff),
            mt_rand(0, 0xffff),
            mt_rand(0, 0xffff),
            mt_rand(0, 0x0fff) | 0x4000,
            mt_rand(0, 0x3fff) | 0x8000,
            mt_rand(0, 0xffff),
            mt_rand(0, 0xffff),
            mt_rand(0, 0xffff)
        );
    }

}

